package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class HbaseCliDemo {
    public static void main(String[] args) throws Exception {
        //获取hbase的配置信息对象
        Configuration conf = HBaseConfiguration.create();
        //设置配置zk的具体信息
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        //获取到hbase的连接对象
        Connection conn = ConnectionFactory.createConnection(conf);

        //获取到hbase的操作对象
        Admin admin = conn.getAdmin();
        //列出所有的名称空间
        /*NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
            String namespace = namespaceDescriptor.getName();
            System.out.println(namespace);
        }*/

        //创建一个名称空间
        /*NamespaceDescriptor.Builder java = NamespaceDescriptor.create("java");
        java.addConfiguration("describe","this namespace from idea");

        NamespaceDescriptor build = java.build();

        admin.createNamespace(build);
*/

        //删除一个名称空间
//        admin.deleteNamespace("java");

        //创建一张表
        /*TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("test:asss"));
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("f1".getBytes(StandardCharsets.UTF_8));
        columnFamilyDescriptorBuilder.setTimeToLive(20);
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        //构建一个表的描述器
        TableDescriptor build = tableDescriptorBuilder.build();
        //创建一张表
        admin.createTable(build);*/

        //往表里面put数据
        Table table = conn.getTable(TableName.valueOf("test:asss"));
        //delete数据
       /* Delete delete = new Delete("rowkey001".getBytes(StandardCharsets.UTF_8));
        CellBuilder abc = CellBuilderFactory.create(CellBuilderType.valueOf("abc"));
        Cell build = abc.build();
        Cell cell = CellUtil.createCell("rowkey001".getBytes(StandardCharsets.UTF_8), "f1".getBytes(StandardCharsets.UTF_8), "name".getBytes(StandardCharsets.UTF_8));
        delete.add(cell);
        table.delete(delete);*/
        Get get = new Get("rowkey001".getBytes(StandardCharsets.UTF_8));

        //get数据
        Result result = table.get(get);
        if (result.advance()){
            Cell current = result.current();
            String family = new String(CellUtil.cloneFamily(current));
        }

        Scan scan = new Scan();
        PrefixFilter prefixFilter = new PrefixFilter("abc".getBytes(StandardCharsets.UTF_8));
        scan.setFilter(prefixFilter);
        scan.addFamily("f1".getBytes(StandardCharsets.UTF_8));

        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();

        while (iterator.hasNext()){
            Result next = iterator.next();
            while (next.advance()){

            }
        }





        /*Put put = new Put("rowkey001".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        table.put(put);*/
        //插入数据

        //buffer的形式插入数据
        /*BufferedMutator bufferedMutator = conn.getBufferedMutator(TableName.valueOf("test:asss"));

        ArrayList<Put> puts = new ArrayList<>();

        Put put = new Put("rowker002".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"gender".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"abc".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"bcdd".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put.addColumn("f1".getBytes(StandardCharsets.UTF_8),"abcd".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));

        Put put1 = new Put("rowker003".getBytes(StandardCharsets.UTF_8));
        put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"gender".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"abc".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"bcdd".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put1.addColumn("f1".getBytes(StandardCharsets.UTF_8),"abcd".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        Put put2 = new Put("rowker004".getBytes(StandardCharsets.UTF_8));
        put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"gender".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"abc".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"bcdd".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));
        put2.addColumn("f1".getBytes(StandardCharsets.UTF_8),"abcd".getBytes(StandardCharsets.UTF_8),"zss".getBytes(StandardCharsets.UTF_8));

        puts.add(put1);
        puts.add(put2);
        puts.add(put);

        bufferedMutator.mutate(puts);
        bufferedMutator.flush();*/

        conn.close();
    }
}
